package com.zerone.dy.demomsg.service;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.zerone.dy.demomsg.entity.JbGroup;
import com.zerone.dy.demomsg.entity.JbGroupUsers;
import com.zerone.dy.demomsg.entity.UserInfo;
import com.zerone.dy.demomsg.entity.dto.UserGroupsDto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.aggregation.*;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.stereotype.Service;

import javax.swing.text.Document;
import java.util.List;
import java.util.Map;

@Service
public class JbGroupService {
    @Autowired
    private MongoTemplate mongoTemplate;

    public void saveOrUpdateGroup(JbGroup jbGroup){
        mongoTemplate.save(jbGroup);
    }

    public void del(String groupCode){
        JbGroup jbGroup = this.findByGroupCode(groupCode);
        if(jbGroup==null){
            return;
        }
        jbGroup.setStats(-1);
        this.saveOrUpdateGroup(jbGroup);
    }


    public JbGroup findByGroupCode(String groupCode){
        JbGroup jbGroup = mongoTemplate.findById(groupCode, JbGroup.class);
        return jbGroup;
    }

    public List findByAcNo(String acNo) {
        Aggregation aggregation = Aggregation.newAggregation(
                Aggregation.match(Criteria.where("acNo").is(acNo)),  // 在此处添加筛选条件，如果需要的话
                Aggregation.lookup("JbGroup", "groupCode", "_id", "jbGroup") // 连接B表并存储到"b"字段
        );

        AggregationResults<UserGroupsDto> results = mongoTemplate.aggregate(aggregation, JbGroupUsers.class, UserGroupsDto.class);

        return results.getMappedResults();
    }

}
